﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Photon.Pun;
using DG.Tweening;

namespace Assets.Scripts.UI
{
    public class UIBelt_Item : MonoBehaviour, IPointerClickHandler
    {
        public Transform qiIcon;

        public Image CardImage;

        private void Start()
        {
            if (qiIcon != null)
            {
                Tweener tweener = qiIcon.DOLocalMove(new Vector3(0, 230, 0), 0.5f);
                tweener.SetAutoKill(false);
                tweener.Pause();
            }
        }

        private bool isSelect;
        public bool IsSelect
        {
            get => isSelect;

            set
            {
                isSelect = value;
                if (isSelect)
                {
                    CardImage.transform.localPosition = new Vector3(0, 100, 0);
                    if (qiIcon != null)
                    {
                        qiIcon.DOPlayForward(); ;    // 气icon的动画
                    }

                    if (this.cardID == 3)
                    {
                        UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(false);
                        UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(false);
                        // TODO:如果当前抽的是普通牌堆，那就只移动一张背面图，不显露图像，等玩家点击了确定按钮后再显示图片

                    }
                    CardImage.color = Color.white;

                    #region 根据玩家阵营，显示不同的tip来通知玩家是获得一点气还是失去一点气
                    if (this.cardID == 0)
                    {
                        UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(false);
                        UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(false);

                        if (PhotonNetwork.IsMasterClient)
                        {
                            UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(true);
                        }
                        UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(true);
                    }
                    if (this.cardID == 2)
                    {
                        UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(false);
                        UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(false);

                        if (PhotonNetwork.IsMasterClient)
                        {
                            UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(true);
                        }
                        UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(true);
                    }
                    if (this.cardID == 1)
                    {
                        UIBelt_Panel.Instance.getQiTip.gameObject.SetActive(false);
                        UIBelt_Panel.Instance.loseQiTip.gameObject.SetActive(false);
                    }
                    #endregion

                }
                else
                {
                    if (qiIcon != null)
                    {
                        qiIcon.DOPlayBackwards();
                    }

                    CardImage.transform.localPosition = Vector3.zero;
                }
            }
        }

        public int cardID;  // 公开出去的抽卡阶段id，0是红方，1是中间，2是蓝方 ，3是普通牌堆

        public void Init(int cardID, CardDefine cardDefine)
        {
            this.cardID = cardID;
            CardImage.sprite = cardDefine.cardImage;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            UIBelt_Panel.Instance.SelectBeltCard(cardID);
        }
    }
}
